from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, ValidationError, Regexp, EqualTo
from models import User


class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    submit = SubmitField('登陆')


class RegisterForm(FlaskForm):
    uname = StringField(
        validators=[
            DataRequired('请输入用户名')
        ],
        description="用户名",
        # 附加选项,会自动在前端判别
        render_kw={
            "class": "form-control",
            "placeholder": "请输入用户名",
        }
    )

    tel = StringField(
        validators=[
            DataRequired('请输入手机号码'),
            Regexp("1[3578]\d{9}", message="手机号码格式不正确")
        ],
        description="手机",
        # 附加选项,会自动在前端判别
        render_kw={
            "class": "form-control",
            "placeholder": "请输入手机号码",
        }
    )

    pwd = PasswordField(
        validators=[
            DataRequired('请输入密码')
        ],
        description="密码",
        # 附加选项,会自动在前端判别
        render_kw={
            "class": "form-control",
            "placeholder": "请输入密码!",
        }
    )

    conf_pwd = PasswordField(
        validators=[
            DataRequired('确认密码'),
            EqualTo('pwd', message="两次密码输入不一致")
        ],
        description="确认密码",
        # 附加选项,会自动在前端判别
        render_kw={
            "class": "form-control",
            "placeholder": "确认密码",
        }
    )
    submit = SubmitField(
        render_kw={
            "class": "btn btn-success btn-block",
        }
    )


class ChangePwdForm(FlaskForm):

    old_pwd = PasswordField(
        validators=[
            DataRequired('请输入旧密码')
        ],
        description="旧密码",
        # 附加选项,会自动在前端判别
        render_kw={
            "class": "form-control",
            "placeholder": "请输入旧密码!",
        }
    )

    new_pwd = PasswordField(
        validators=[
            DataRequired('请输入新密码')
        ],
        description="新密码",
        # 附加选项,会自动在前端判别
        render_kw={
            "class": "form-control",
            "placeholder": "请输入新密码!",
        }
    )

    conf_pwd = PasswordField(
        validators=[
            DataRequired('确认新密码'),
            EqualTo('new_pwd', message="两次密码输入不一致")
        ],
        description="确认新密码",
        # 附加选项,会自动在前端判别
        render_kw={
            "class": "form-control",
            "placeholder": "确认新密码",
        }
    )
    submit = SubmitField(
        render_kw={
            "class": "btn btn-success btn-block",
        }
    )